package qeb.handheldmedical.model;

import java.lang.reflect.Field;

import org.json.JSONException;
import org.json.JSONObject;

public class BaseModel {

    public void getValue(JSONObject jobj) throws InstantiationException {
        for (Field field : this.getClass().getFields()) {
            try {
                if (jobj.getString(field.getName()) != null) {
                    field.set(this, jobj.getString(field.getName()));
                }
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                //e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                //e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                //e.printStackTrace();
            }

        }
    }

    public JSONObject toJSONObject() {
        JSONObject jobj = new JSONObject();
        for (Field field : this.getClass().getFields()) {
            try {
                jobj.put(field.getName(), field.get(this));
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return jobj;
    }
}
